package Begin;

/**
 * @Description:多线程编程的基础测试以及学习
 * @Author:AMark
 * @Date: 10/26/2023
 */
public class ThreadDemo2 {
    //测试线程的主函数
//    private static class MyRunnable implements Runnable {
//        //中断的第一种方式，通过基本变量进行中断
////        public volatile boolean isquit = false;
////
////        @Override
////        public void run() {
////            while (!isquit) {
////                System.out.println(Thread.currentThread().getName() + "正在转账");
////                try {
////                    Thread.sleep(1000);
////                } catch (InterruptedException e) {
////                    e.printStackTrace();
////                }
////            }
////            System.out.println(Thread.currentThread().getName() + ":啊，险些误了大事");
////        }
//        //中断的第二种方式，通过内置的interrupt函数进行中断
////        @Override
////        public void run() {
////            while(!Thread.interrupted()) {
////                System.out.println(Thread.currentThread().getName() + "Leave me alone, I am busy!");
////                try {
////                    Thread.sleep(1000);
////                } catch (InterruptedException e) {
////                    e.printStackTrace();
////                    System.out.println(Thread.currentThread().getName() + "有内鬼，终止交易");
////                    break;
////                }
////            }
////            System.out.println(Thread.currentThread().getName() + ":啊，险些误了大事");
////        }
//        //等待一个线程
//
//    }
    public static void main(String[] args) throws InterruptedException {
        Runnable targert = () -> {
            for (int i = 0; i < 10; i++) {
                try {
                    System.out.println(Thread.currentThread().getName() + ":我还在工作");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + ":我结束工作了");
        };
        Thread thread1 = new Thread(targert, "张禾川");
        Thread thread2 = new Thread(targert, "Mark");
        System.out.println("先让张禾川开始工作");
        thread1.start();
        thread1.join();
        System.out.println("张禾川工作结束");
        thread2.start();
        thread2.join();
        System.out.println("Mark's work has been done");
    }
    //针对第一个线程演示以及第二个线程演示的主函数
//    public static void main(String[] args) throws InterruptedException {
//        MyRunnable target = new MyRunnable();
//        Thread thread = new Thread(target, "张禾川");
//        System.out.println(Thread.currentThread().getName() + ":让张禾川开始转账");
//        thread.start();
//        Thread.sleep(3*1000);
//        System.out.println(Thread.currentThread().getName() + ":老板来了，赶紧通知张禾川");
//        thread.interrupt();
////        target.isquit = true;
//    }
}
